home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / board / Crafty-15.19.lha / crafty-15.19 / src / init.c < prev    next >
C/C++ Source or Header  |  1998-09-26  |  51KB  |  1,605 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "chess.h"
  5. #include "data.h"
  6. #if defined(UNIX) || defined(AMIGA)
  7. #  include <unistd.h>
  8. #endif
  9. #include "epdglue.h"
  10. #if defined(NT_i386) || defined(NT_AXP)
  11. #  include <fcntl.h>  /* needed for definition of "_O_BINARY" */
  12. #endif
  13. #if defined(AMIGA)
  14. #include <proto/exec.h>
  15. #include <exec/memory.h>
  16. static unsigned long largest_chunk ;
  17. #endif
  18.  
  19. #if defined(COMPACT_ATTACKS)
  20. extern unsigned char init_l90[];
  21. extern unsigned char init_l45[];
  22. extern unsigned char init_r45[];
  23. #else
  24. int init_r90[64] = { 56, 48, 40, 32, 24, 16,  8,  0,
  25.                      57, 49, 41, 33, 25, 17,  9,  1,
  26.                      58, 50, 42, 34, 26, 18, 10,  2,
  27.                      59, 51, 43, 35, 27, 19, 11,  3,
  28.                      60, 52, 44, 36, 28, 20, 12,  4,
  29.                      61, 53, 45, 37, 29, 21, 13,  5,
  30.                      62, 54, 46, 38, 30, 22, 14,  6,
  31.                      63, 55, 47, 39, 31, 23, 15,  7 };
  32.  
  33. int init_l90[64] = {  7, 15, 23, 31, 39, 47, 55, 63,
  34.                       6, 14, 22, 30, 38, 46, 54, 62,
  35.                       5, 13, 21, 29, 37, 45, 53, 61,
  36.                       4, 12, 20, 28, 36, 44, 52, 60,
  37.                       3, 11, 19, 27, 35, 43, 51, 59,
  38.                       2, 10, 18, 26, 34, 42, 50, 58,
  39.                       1,  9, 17, 25, 33, 41, 49, 57,
  40.                       0,  8, 16, 24, 32, 40, 48, 56 };
  41.  
  42. int init_l45[64] = {                0,
  43.                                   2,  5,
  44.                                 9, 14, 20,
  45.                              27, 35,  1,  4,
  46.                             8, 13, 19, 26, 34,
  47.                          42,  3,  7, 12, 18, 25,
  48.                        33, 41, 48,  6, 11, 17, 24,
  49.                      32, 40, 47, 53, 10, 16, 23, 31,
  50.                        39, 46, 52, 57, 15, 22, 30,
  51.                          38, 45, 51, 56, 60, 21,
  52.                            29, 37, 44, 50, 55,
  53.                              59, 62, 28, 36,
  54.                                43, 49, 54,
  55.                                  58, 61,
  56.                                    63 };
  57.  
  58. int init_ul45[64] = {               0,
  59.                                   8,  1,
  60.                                16,  9,  2,
  61.                              24, 17, 10,  3,
  62.                            32, 25, 18, 11,  4,
  63.                          40, 33, 26, 19, 12,  5,
  64.                        48, 41, 34, 27, 20, 13,  6,
  65.                      56, 49, 42, 35, 28, 21, 14,  7,
  66.                        57, 50, 43, 36, 29, 22, 15,
  67.                          58, 51, 44, 37, 30, 23,
  68.                            59, 52, 45, 38, 31,
  69.                              60, 53, 46, 39,
  70.                                61, 54, 47,
  71.                                  62, 55,
  72.                                    63 };
  73.  
  74. int init_r45[64] = {               28,
  75.                                  21, 15,
  76.                                10,  6,  3,
  77.                               1,  0, 36, 29,
  78.                            22, 16, 11,  7,  4,
  79.                           2, 43, 37, 30, 23, 17,
  80.                        12,  8,  5, 49, 44, 38, 31,
  81.                      24, 18, 13,  9, 54, 50, 45, 39,
  82.                        32, 25, 19, 14, 58, 55, 51,
  83.                          46, 40, 33, 26, 20, 61,
  84.                            59, 56, 52, 47, 41,
  85.                              34, 27, 63, 62,
  86.                                60, 57, 53,
  87.                                  48, 42,
  88.                                    35  };
  89.  
  90. int init_ur45[64] = {               7,
  91.                                   6, 15,
  92.                                 5, 14, 23,
  93.                               4, 13, 22, 31,
  94.                             3, 12, 21, 30, 39,
  95.                           2, 11, 20, 29, 38, 47,
  96.                         1, 10, 19, 28, 37, 46, 55,
  97.                       0,  9, 18, 27, 36, 45, 54, 63,
  98.                         8, 17, 26, 35, 44, 53, 62,
  99.                          16, 25, 34, 43, 52, 61,
  100.                            24, 33, 42, 51, 60,
  101.                              32, 41, 50, 59,
  102.                                40, 49, 58,
  103.                                  48, 57,
  104.                                    56 };
  105.  
  106. int diagonal_length[64] = {         1,
  107.                                   2,  2,
  108.                                 3,  3,  3,
  109.                               4,  4,  4,  4,
  110.                             5,  5,  5,  5,  5,
  111.                           6,  6,  6,  6,  6,  6,
  112.                         7,  7,  7,  7,  7,  7,  7,
  113.                       8,  8,  8,  8,  8,  8,  8,  8,
  114.                         7,  7,  7,  7,  7,  7,  7,
  115.                           6,  6,  6,  6,  6,  6,
  116.                             5,  5,  5,  5,  5,
  117.                               4,  4,  4,  4,
  118.                                 3,  3,  3,
  119.                                   2,  2,
  120.                                     1 };
  121. #endif
  122.  
  123. void Initialize(int continuing) {
  124. /*
  125.  ----------------------------------------------------------
  126. |                                                          |
  127. |   perform routine initialization.                        |
  128. |                                                          |
  129.  ----------------------------------------------------------
  130. */
  131.   int i, j, major, minor;
  132.   TREE *tree;
  133.  
  134. #if defined(SMP)
  135.   for (i=1;i<MAX_BLOCKS+1;i++) {
  136.     local[i]=(TREE*) malloc(sizeof(TREE));
  137.     local[i]->used=0;
  138.   }
  139.   local[0]->parent=(TREE*)-1;
  140. #endif
  141.  
  142.   tree=local[0];
  143.   i=0;
  144.   InitializeZeroMasks();
  145. #if defined(SMP)
  146.   InitializeSMP();
  147. #endif
  148.   InitializeMasks();
  149.   InitializeRandomHash();
  150.   InitializeAttackBoards();
  151.   InitializePawnMasks();
  152.   InitializePieceMasks();
  153.   InitializeChessBoard(&tree->position[0]);
  154. #if defined(NT_i386) || defined(NT_AXP)
  155.   _fmode = _O_BINARY;  /* set global file mode to binary to avoid text translation */
  156. #endif
  157.  
  158.   EGInit();
  159.  
  160.   tree->last[0]=tree->move_list;
  161.  
  162. #if defined(MACOS)
  163.   sprintf(log_filename,":%s:book.bin",book_path);
  164. #else
  165.   sprintf(log_filename,"%s/book.bin",book_path);
  166. #endif
  167.   book_file=fopen(log_filename,"rb+");
  168.   if (!book_file) {
  169.     book_file=fopen(log_filename,"rb");
  170.     if (!book_file) {
  171. #if defined(MACOS)
  172.       printf("unable to open book file [:%s:book.bin].\n",book_path);
  173. #else
  174.       printf("unable to open book file [%s/book.bin].\n",book_path);
  175. #endif
  176.       printf("book is disabled\n");
  177.     }
  178.     else {
  179. #if defined(MACOS)
  180.       printf("unable to open book file [:%s:book.bin] for \"write\".\n",book_path);
  181. #else
  182.       printf("unable to open book file [%s/book.bin] for \"write\".\n",book_path);
  183. #endif
  184.       printf("learning is disabled\n");
  185.     }
  186.   }
  187. #if defined(MACOS)
  188.   sprintf(log_filename,":%s:books.bin",book_path);
  189. #else
  190.   sprintf(log_filename,"%s/books.bin",book_path);
  191. #endif
  192.   books_file=fopen(log_filename,"rb");
  193. #if defined(MACOS)
  194.   if (!books_file) printf("unable to open book file [:%s:books.bin].\n",book_path);
  195. #else
  196.   if (!books_file) printf("unable to open book file [%s/books.bin].\n",book_path);
  197. #endif
  198.   if (book_file) {
  199.     fseek(book_file,-sizeof(int),SEEK_END);
  200.     fread(&major,sizeof(int),1,book_file);
  201.     minor=major&65535;
  202.     major=major>>16;
  203.     if (major<15 || (major==15 && minor<15)) {
  204.       Print(4095,"\nERROR!  book.bin not made by version 15.15 or later\n");
  205.       book_file=0;
  206.       books_file=0;
  207.     }
  208.   }
  209. #if defined(MACOS)
  210.   sprintf(log_filename,":%s:book.lrn",book_path);
  211. #else
  212.   sprintf(log_filename,"%s/book.lrn",book_path);
  213. #endif
  214.   book_lrn_file=fopen(log_filename,"a");
  215.   if (!book_lrn_file) {
  216. #if defined(MACOS)
  217.     printf("unable to open book learning file [:%s:book.lrn].\n",book_path);
  218. #else
  219.     printf("unable to open book learning file [%s/book.lrn].\n",book_path);
  220. #endif
  221.     printf("learning disabled.\n");
  222.     learning&=~(book_learning+result_learning);
  223.   }
  224.   if (learning&position_learning) {
  225. #if defined(MACOS)
  226.     sprintf(log_filename,":%s:position.bin",book_path);
  227. #else
  228.     sprintf(log_filename,"%s/position.bin",book_path);
  229. #endif
  230.     position_file=fopen(log_filename,"rb+");
  231.     if (position_file) {
  232.       fseek(position_file,0,SEEK_END);
  233.       if (ftell(position_file) == 0) {
  234.         fclose(position_file);
  235.         position_file=0;
  236.       }
  237.     }
  238.     if (!position_file) {
  239.       position_file=fopen(log_filename,"wb+");
  240.       if (positio